﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Collections.Generic;
using System.Collections;
namespace WPFExcelReport
{

    public partial class VYGRONAV95
    {

        public DataSet PlanTAB50(DataSet SetEnv, DataSet DB, DataRow dwABA, DataRow dwRule, double VKPQTY)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            DataRow[] rows = null;
            DataRow dwGSDAT = null;
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataRow dwMU = null;
            double RQTY = 0;
            string MU = "  ";

            #region========Validate=================
            string KRS0 = dwABA["ABAAUFKRS"].ToString();
            string POSTYP = "  ";
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            string POS0 = dwABA["ABAAUFPOS"].ToString();

            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();

            filter = "GSART='" + ART + "' AND GSVAR='" + VAR + "'";
            rows = DB.Tables["GSDAT"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwGSDAT = row;
                break;
            }
            if (dwGSDAT == null)
            {
                throw new Exception("纱线" + ART + "版本" + VAR + "档案不存在");
            }
            if (dwGSDAT["GSFERTKZ"].ToString() != "E"
                & dwGSDAT["GSFERTKZ"].ToString() != "F"
                & dwGSDAT["GSFERTKZ"].ToString() != "X"
                )
            {
                var query1 = from DataRow row in tbABAUFM.AsEnumerable()
                             where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                             select row;
                foreach (DataRow row in query1)
                {
                    row["ABAAUFTYP"] = "V";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "000000";
                    row["ABAAUFPOS"] = "000";
                }
                return SReturn;
            }
            APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
            APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
            filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
            string sortby = "APFAFOLGE ASC";
            rows = DB.Tables["APFOLGE"].Select(filter, sortby);
            foreach (DataRow row in rows)
            {
                PRLO = row["KSZLO"].ToString();
                PRLB = row["KSZLB"].ToString();
                PRLP = row["KSZLP"].ToString();
                break;
            }
            #endregion
            #region=====Rules============
            QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0;
            string SMU = "  ";
            string SPACK = "    ";
            if (dwRule != null)
            {
                //用于计算下级原料
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                R02 = Convert.ToDouble(dwRule["R02"].ToString()) / 100;
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                F02 = Convert.ToDouble(dwRule["F02"].ToString());
                DELDATE = dwRule["DELDATE"].ToString();
                STARTDATE = dwRule["STARTDATE"].ToString();
                LDAB50 = Convert.ToDecimal(dwRule["LEADTIME"].ToString());

                POSTYP = Helpers.Helper.Right(dwRule["COLOR"].ToString().Trim().PadRight(3), 3);
                QTYEND = Convert.ToDouble(dwRule["QTYEND"].ToString());
                QTYMAT = Convert.ToDouble(dwRule["QTYMAT"].ToString());
                MU = Helpers.Helper.Left(dwRule["R01DES"].ToString().Trim().PadRight(2), 2);//代表粗纱等定量和内包装
                SMU = Helpers.Helper.Left(dwRule["R02DES"].ToString().Trim().PadRight(2), 2);
                SPACK = Helpers.Helper.Left(dwRule["F01DES"].ToString().Trim().PadRight(4), 4);
            }
            if (QTYEND < 0)
            {
                QTYEND = Convert.ToDouble(dwABA["ABARESMG"].ToString());//无放大系数 
            }
            if (DELDATE.Trim().Length < 8)
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DELDATE;
            }

            if (STARTDATE.Trim().Length < 8)
            {
                STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB50));
            }
            else
            {
                STARTD = STARTDATE;
            }
            if (STARTD.Trim() == "") STARTD = "0";
            QTYMAT = QTYEND;
            double TargetRatio = QTYMAT / QTYEND;//例如1.2，就是100kg成品需要120kg原料
            if (TargetRatio < 0)
                TargetRatio = 1;
            #endregion
            #region=======dwABPN==========
            dwABPN = tmpABPOS.Rows.Add();
            dwABPN["ABPAENDZL"] = 2;
            dwABPN["ABPMANDANT"] = "00";
            dwABPN["ABPAUFART"] = "GP";
            dwABPN["ABPAUFKRS"] = KRS0;
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPZLO"] = PRLO;
            dwABPN["ABPZLB"] = PRLB;
            dwABPN["ABPSME"] = "5";
            dwABPN["ABPPOSTYP"] = POSTYP;
            dwABPN["ABPWAKZ"] = "G";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPVTDATE"] = ENDD;
            dwABPN["ABPENDDAT"] = ENDD;
            dwABPN["ABPENDDATA"] = ENDD;
            dwABPN["ABPLIEFDATN"] = ENDD;
            dwABPN["ABPTERMDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDATA"] = STARTD;
            dwABPN["ABPSTARTDATN"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSOLLMENGE1"] = 0;
            dwABPN["ABPSOLLMENGE2"] = 0;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = QTYEND * 100;
            dwABPN["ABPSOLLMENGE6"] = 0;
            dwABPN["ABPSOLLMENGE7"] = VKPQTY * 100;
            dwABPN["ABPSOLLMENGE8"] = 0;
            dwABPN["ABPZUGKG"] = 0;
            dwABPN["ABPSTKLG"] = Math.Round(TargetRatio * 10000000);
            //BASEMG2=1.4*0.2
            //GAKG1=ABPQTY*BASEMG2
            #endregion

            #region=======ICC_ABAX ================
            //上层的需求,销售单是上层，细纱单是本层
            var query = from DataRow row in tbABAUFM.AsEnumerable()
                        where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABAQTYRESERVE"] = dwABA["ABARESMG"].ToString();
                row["ABAAUFMSL"] = MU;
            }
            //产生下层需求
            #region============GAAUF732====================
            DataRow dwGASTGA = null;
            double GAKG1 = 0;
            double RT = 0;
            filter = "GAGART='" + ART + "' AND GAGVAR='" + VAR + "' AND GAGFARBE='" + FARBE + "'";
            string sort = " GAGPOS ASC";
            rows = DB.Tables["GASTGA"].Select(filter, sort);
            if (rows.Length == 0)
            {
                MessageBox.Show(filter + "没有BOM");
                throw new Exception(filter + "没有BOM");
            }
            string addedpos = "";
            foreach (DataRow row in rows)
            {
                dwGASTGA = row;

                string YPOS = dwGASTGA["GAGPOS"].ToString();
                if (addedpos.Contains(YPOS + ";"))
                    continue;
                addedpos += YPOS + ";";

                string GART = dwGASTGA["GAGGART"].ToString();
                string GVAR = dwGASTGA["GAGGVAR"].ToString();
                string GFARBE = dwGASTGA["GAGGFARBE"].ToString();
                if (GART.StartsWith("3"))
                {
                    throw new Exception("请检查"+ART+"股线定义！");
                  
                }
                if (GVAR.Trim() == "")
                {
                    SResult = INTEXDBC.QueryGSCOLOR(dwGASTGA, 7);

                    foreach (DataRow dwColor in SResult.Tables[0].Rows)
                    {
                        GVAR = dwColor["GSCVAR"].ToString();
                    }

                }
                string GAGFILLER = dwGASTGA["GAGFILLER"].ToString();
                if (GAGFILLER.Trim() != "")
                {
                    SMU = GAGFILLER.Substring(1, 2);
                }
                double GAGKGBRUT = Convert.ToDouble(dwGASTGA["GAGANTEIL"].ToString()) * 0.001 * 0.01;
                //BASEMG2=1.4*0.2
                //GAKG1=ABPQTY*BASEMG2

                if (GAGKGBRUT == 0)
                    GAGKGBRUT = 1;
                double GABASMG2 = 0;
                GABASMG2 = TargetRatio * GAGKGBRUT;
                GAKG1 = (QTYEND - F02 + F01) * (1 + R01) * GABASMG2;
                dwMU = tbABAUFM.Rows.Add();
                dwMU["ABAID"] = ABAID; ABAID++;
                #region=====MapRules=======
                dwMU["QTYEND"] = QTYEND;
                dwMU["DELDATE"] = ENDD;
                dwMU["STARTDATE"] = STARTD;
                dwMU["LEADTIME"] = dwRule["LEADTIME"];
                dwMU["ABACOLORREF"] = dwRule["ORDERNUM"];
                dwMU["UNITNUM"] = dwRule["UNITNUM"];
                dwMU["TTENDS"] = dwRule["TTENDS"];
                dwMU["UENDS"] = dwRule["UENDS"];
                dwMU["UNDES"] = dwRule["UNDES"];
                dwMU["UNITQTY"] = dwRule["UNITQTY"];
                dwMU["R01"] = dwRule["R01"];
                dwMU["R01DES"] = dwRule["R01DES"];
                dwMU["R02"] = dwRule["R02"];
                dwMU["R02DES"] = dwRule["R02DES"];
                dwMU["F01"] = dwRule["F01"];
                dwMU["F01DES"] = dwRule["F01DES"];
                dwMU["F02"] = dwRule["F02"];
                dwMU["F02DES"] = dwRule["F02DES"];
                dwMU["JNUM"] = dwRule["JNUM"];
                dwMU["JWASTGE"] = dwRule["JWASTGE"];
                dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
                dwMU["UMS"] = dwRule["UMS"];
                #endregion
                dwMU["ABAMANDANT"] = "00";
                dwMU["ABAAUFTYP"] = "N";
                dwMU["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFTYP"] = "V";
                dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
                dwMU["ABAVKUPOS"] = dwGASTGA["GAGPOS"];
                dwMU["ABPWAKZ"] = "G";
                dwMU["ABPART"] = ART;
                dwMU["ABPVAR"] = VAR;
                dwMU["ABPFARBE"] = FARBE;
                dwMU["EXART"] = dwABA["EXART"];
                dwMU["ABAWAKZ"] = "G";
                dwMU["ABAART"] = GART;
                dwMU["ABAVAR"] = GVAR;
                dwMU["ABAFARBE"] = GFARBE;
                dwMU["ABASSTKLG"] = 0;
                dwMU["ABARESMG"] = Math.Ceiling(GAKG1 * 1000);
                dwMU["ABASANZSTK"] = Math.Ceiling(GAKG1);
                dwMU["ABASSTKGEW"] = "1000";
                dwMU["ABAAUFMSL"] = SMU; ; //GAAUFMSL
                dwMU["ABAPACKTYP"] = SPACK; ;
                dwMU["ABAHUELSE"] = "      ";
                dwMU["ABAVKFARBE"] = Math.Round(GABASMG2 * 1000 * 100);//100000
                dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
                dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
                dwMU["ABABASMG"] = dwGASTGA["GAGBASIS"];
                dwMU["ABADISPOKZ"] = dwGASTGA["GAGDISPOKZ"];
                dwMU["ABAQTYALLOCATE"] = 0;
                dwMU["ABAQTYRECEIVED"] = 0;
                dwMU["ABALO"] = PRLO;
                dwMU["ABALB"] = PRLB;
                dwMU["ABALP"] = PRLP;
                dwMU["ABAUSER"] = app.User;
                dwMU["ABADATE"] = date; ;
            }
            #endregion
            #endregion
            SReturn.Merge(tmpABPOS);
            return SReturn;
        }
        public DataSet PlanXAB50(DataSet SetEnv, DataSet DB, DataRow dwABA, DataRow dwRule, double VKPQTY)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            DataRow[] rows = null;
            DataRow dwGSDAT = null;
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataRow dwMU = null;
            double RQTY = 0;
            string MU = "  ";

            #region========Validate=================
            string KRS0 = dwABA["ABAAUFKRS"].ToString();
            string POSTYP = "  ";
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            string POS0 = dwABA["ABAAUFPOS"].ToString();

            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();

            filter = "GSART='" + ART + "' AND GSVAR='" + VAR + "'";
            rows = DB.Tables["GSDAT"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwGSDAT = row;
                break;
            }
            if (dwGSDAT == null)
            {
                throw new Exception("纱线" + ART + "版本" + VAR + "档案不存在");
            }
            if (dwGSDAT["GSFERTKZ"].ToString() != "E"
                & dwGSDAT["GSFERTKZ"].ToString() != "F"
                & dwGSDAT["GSFERTKZ"].ToString() != "X"
                )
            {
                var query1 = from DataRow row in tbABAUFM.AsEnumerable()
                             where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                             select row;
                foreach (DataRow row in query1)
                {
                    row["ABAAUFTYP"] = "V";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "000000";
                    row["ABAAUFPOS"] = "000";
                }
                throw new Exception("纱线" + ART + "版本" + VAR + "档案不是生产类型纱线");
                return SReturn;
            }
            APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
            APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
            filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
            string sortby = "APFAFOLGE ASC";
            rows = DB.Tables["APFOLGE"].Select(filter, sortby);
            foreach (DataRow row in rows)
            {
                PRLO = row["KSZLO"].ToString();
                PRLB = row["KSZLB"].ToString();
                PRLP = row["KSZLP"].ToString();
                break;
            }
            #endregion
            #region=====Rules============
            QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0;
            string SMU = "  ";
            string SPACK = "    ";
            if (dwRule != null)
            {
                //用于计算下级原料
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                R02 = Convert.ToDouble(dwRule["R02"].ToString()) / 100;
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                F02 = Convert.ToDouble(dwRule["F02"].ToString());
                DELDATE = dwRule["DELDATE"].ToString();
                STARTDATE = dwRule["STARTDATE"].ToString();
                LDAB50 = Convert.ToDecimal(dwRule["LEADTIME"].ToString());

                POSTYP = Helpers.Helper.Right(dwRule["COLOR"].ToString().Trim().PadRight(3), 3);
                QTYEND = Convert.ToDouble(dwRule["QTYEND"].ToString());
                QTYMAT = Convert.ToDouble(dwRule["QTYMAT"].ToString());
                MU = Helpers.Helper.Left(dwRule["R01DES"].ToString().Trim().PadRight(2), 2);//代表粗纱等定量和内包装
                SMU = Helpers.Helper.Left(dwRule["R02DES"].ToString().Trim().PadRight(2), 2);
                SPACK = Helpers.Helper.Left(dwRule["F01DES"].ToString().Trim().PadRight(4), 4);
            }
            if (QTYEND < 0)
            {
                QTYEND = Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 1000;//无放大系数 
            }
            if (DELDATE.Trim().Length < 8)
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DELDATE;
            }

            if (STARTDATE.Trim().Length < 8)
            {
                STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB50));
            }
            else
            {
                STARTD = STARTDATE;
            }
            if (STARTD.Trim() == "") STARTD = "0";
            if (QTYMAT < 0)
            {
                QTYMAT = (QTYEND - F02 + F01) * (1 + R01);
            }
            double TargetRatio = QTYMAT / QTYEND;//例如1.2，就是100kg成品需要120kg原料
            if (TargetRatio < 0)
                TargetRatio = 1;
            #endregion
            #region=======dwABPN==========
            dwABPN = tmpABPOS.Rows.Add();
            dwABPN["ABPAENDZL"] = 2;
            dwABPN["ABPMANDANT"] = "00";
            dwABPN["ABPAUFART"] = "GP";
            dwABPN["ABPAUFKRS"] = KRS0;
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPZLO"] = PRLO;
            dwABPN["ABPZLB"] = PRLB;
            dwABPN["ABPSME"] = "5";
            dwABPN["ABPPOSTYP"] = POSTYP;
            dwABPN["ABPWAKZ"] = "G";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPVTDATE"] = ENDD;
            dwABPN["ABPENDDAT"] = ENDD;
            dwABPN["ABPENDDATA"] = ENDD;
            dwABPN["ABPLIEFDATN"] = ENDD;
            dwABPN["ABPTERMDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDATA"] = STARTD;
            dwABPN["ABPSTARTDATN"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSOLLMENGE1"] = 0;
            dwABPN["ABPSOLLMENGE2"] = 0;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = Math.Ceiling(QTYEND * 100);
            dwABPN["ABPSOLLMENGE6"] = 0;
            dwABPN["ABPSOLLMENGE7"] = VKPQTY * 100;
            dwABPN["ABPSOLLMENGE8"] = 0;
            dwABPN["ABPZUGKG"] = 0;
            dwABPN["ABPSTKLG"] = Math.Round(TargetRatio * 10000000);
            //BASEMG2=1.4*0.2
            //GAKG1=ABPQTY*BASEMG2
            #endregion

            #region=======ICC_ABAX ================
            //上层的需求,销售单是上层，细纱单是本层
            var query = from DataRow row in tbABAUFM.AsEnumerable()
                        where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABAQTYRESERVE"] = dwABA["ABARESMG"].ToString();
                row["ABAAUFMSL"] = MU;
            }
            //产生下层需求
            #region============GAAUF732====================
            DataRow dwGASTGA = null;
            double GAKG1 = 0;
            double RT = 0;
            DataTable tb = null;
            if (SetEnv.Tables.Contains("GASTGA"))
            {
                SetEnv.Tables["GASTGA"].Clear();
            }
            filter = "GAGART='" + ART + "' AND GAGVAR='" + VAR + "' AND GAGFARBE='" + FARBE + "'";
            string sort = " GAGPOS ASC";
            rows = DB.Tables["GASTGA"].Select(filter, sort);
            if (rows.Length == 0)
            {
                MessageBox.Show(filter + "没有BOM");
                throw new Exception(filter + "没有BOM");
            }

            tb = DB.Tables["GASTGA"].Clone();
            tb.TableName = "GASTGA";
            string addedpos = "";
            int count = 0;
            foreach (DataRow row in rows)
            {

                string YPOS = row["GAGPOS"].ToString();
                if (addedpos.Contains(YPOS + ";"))
                    continue;
                dwGASTGA = tb.Rows.Add();
                dwGASTGA.ItemArray = row.ItemArray;
                addedpos += YPOS + ";";
                count++;
                string GART = dwGASTGA["GAGGART"].ToString();
                string GVAR = dwGASTGA["GAGGVAR"].ToString();
                string GFARBE = dwGASTGA["GAGGFARBE"].ToString();
                if (GART.StartsWith("1") || GART.StartsWith("2"))
                    continue;
                if (GVAR.Trim() == "")
                { 
                    SResult = INTEXDBC.QueryGSCOLOR(dwGASTGA, 7);

                    foreach (DataRow dwColor in SResult.Tables[0].Rows)
                    {
                        GVAR = dwColor["GSCVAR"].ToString();
                    }

                }
                string GAGFILLER = dwGASTGA["GAGFILLER"].ToString();
                if (GAGFILLER.Trim() != "")
                {
                    SMU = GAGFILLER.Substring(1, 2);
                }
                double GAGKGBRUT = Convert.ToDouble(dwGASTGA["GAGANTEIL"].ToString()) * 0.001 * 0.01;
                //BASEMG2=1.4*0.2
                //GAKG1=ABPQTY*BASEMG2

                if (GAGKGBRUT == 0)
                    GAGKGBRUT = 1;
                double GABASMG2 = 0;
                GABASMG2 = TargetRatio * GAGKGBRUT;
                GAKG1 = QTYEND * GABASMG2;
                dwMU = tbABAUFM.Rows.Add();
                dwMU["ABAID"] = ABAID; ABAID++;
                #region=====MapRules=======
                dwMU["QTYEND"] = QTYEND;
                dwMU["QTYMAT"] = QTYMAT;
                dwMU["DELDATE"] = ENDD;
                dwMU["STARTDATE"] = STARTD;
                dwMU["LEADTIME"] = dwRule["LEADTIME"];
                dwMU["ABACOLORREF"] = dwRule["ORDERNUM"];
                dwMU["UNITNUM"] = dwRule["UNITNUM"];
                dwMU["TTENDS"] = dwRule["TTENDS"];
                dwMU["UENDS"] = dwRule["UENDS"];
                dwMU["UNDES"] = dwRule["UNDES"];
                dwMU["UNITQTY"] = dwRule["UNITQTY"];
                dwMU["R01"] = dwRule["R01"];
                dwMU["R01DES"] = dwRule["R01DES"];
                dwMU["R02"] = GAGKGBRUT;//保存AB纱的比例
                dwMU["R02DES"] = dwRule["R02DES"];
                dwMU["F01"] = dwRule["F01"];
                dwMU["F01DES"] = dwRule["F01DES"];
                dwMU["F02"] = dwRule["F02"];
                dwMU["F02DES"] = dwRule["F02DES"];
                dwMU["JNUM"] = dwRule["JNUM"];
                dwMU["JWASTGE"] = dwRule["JWASTGE"];
                dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
                dwMU["UMS"] = dwRule["UMS"];
                #endregion
                dwMU["ABAMANDANT"] = "00";
                dwMU["ABAAUFTYP"] = "N";
                dwMU["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFTYP"] = "V";
                dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
                dwMU["ABAVKUPOS"] = dwGASTGA["GAGPOS"];
                dwMU["ABPWAKZ"] = "G";
                dwMU["ABPART"] = ART;
                dwMU["ABPVAR"] = VAR;
                dwMU["ABPFARBE"] = FARBE;
                dwMU["EXART"] = dwABA["EXART"];
                dwMU["ABAWAKZ"] = "G";
                dwMU["ABAART"] = GART;
                dwMU["ABAVAR"] = GVAR;
                dwMU["ABAFARBE"] = GFARBE;
                dwMU["ABASSTKLG"] = 0;
                dwMU["ABARESMG"] = Math.Ceiling(GAKG1 * 1000);
                dwMU["ABASANZSTK"] = Math.Ceiling(GAKG1);
                dwMU["ABASSTKGEW"] = "1000";
                dwMU["ABAAUFMSL"] = SMU; ; //GAAUFMSL
                dwMU["ABAPACKTYP"] = SPACK; ;
                dwMU["ABAHUELSE"] = "      ";
                dwMU["ABAVKFARBE"] = Math.Round(GABASMG2 * 1000 * 100);//100000
                dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
                dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
                dwMU["ABABASMG"] = dwGASTGA["GAGBASIS"];
                dwMU["ABADISPOKZ"] = dwGASTGA["GAGDISPOKZ"];
                dwMU["ABAQTYALLOCATE"] = 0;
                dwMU["ABAQTYRECEIVED"] = 0;
                dwMU["ABALO"] = PRLO;
                dwMU["ABALB"] = PRLB;
                dwMU["ABALP"] = PRLP;
                dwMU["ABAUSER"] = app.User;
                dwMU["ABADATE"] = date; ;
            }
            if (count > 1)
            {  
                
                SetEnv.Merge(tb);
                EventDispatcher.Instance.RaiseStatus("YGRAV95请确认AB纱投料比例", StatusEventType.StopProgress);
            }
            #endregion
            #endregion
            SReturn.Merge(tmpABPOS);
            return SReturn;
        }
        public DataSet PlanRAB50(DataSet SetEnv, DataSet DB, DataRow dwABA, DataRow dwRule, double VKPQTY)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            DataRow[] rows = null;
            DataRow dwGSDAT = null;
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataRow dwMU = null;
            double RQTY = 0;
            string MU = "  ";
            double ABRATIO = 1;
            #region========Validate=================
            string KRS0 = dwABA["ABAAUFKRS"].ToString();
            string POSTYP = "  ";
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            string POS0 = dwABA["ABAAUFPOS"].ToString();

            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();

            filter = "GSART='" + ART + "' AND GSVAR='" + VAR + "'";
            rows = DB.Tables["GSDAT"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwGSDAT = row;
                break;
            }
            if (dwGSDAT == null)
            {
                throw new Exception("纱线" + ART + "版本" + VAR + "档案不存在");
            }
            if (dwGSDAT["GSFERTKZ"].ToString() != "E"
                & dwGSDAT["GSFERTKZ"].ToString() != "F"
                & dwGSDAT["GSFERTKZ"].ToString() != "X"
                )
            {
                var query1 = from DataRow row in tbABAUFM.AsEnumerable()
                             where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                             select row;
                foreach (DataRow row in query1)
                {
                    row["ABAAUFTYP"] = "V";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "000000";
                    row["ABAAUFPOS"] = "000";
                }
                return SReturn;
            }
            APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
            APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
            filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
            string sortby = "APFAFOLGE ASC";
            rows = DB.Tables["APFOLGE"].Select(filter, sortby);
            foreach (DataRow row in rows)
            {
                PRLO = row["KSZLO"].ToString();
                PRLB = row["KSZLB"].ToString();
                PRLP = row["KSZLP"].ToString();
                break;
            }
            #endregion
            #region=====Rules============
            ABRATIO = Convert.ToDouble(dwABA["R02"].ToString());
            QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0;
            string SMU = "  ";
            string SPACK = "    ";
            if (dwRule != null)
            {
                //用于计算下级原料
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                R02 = Convert.ToDouble(dwRule["R02"].ToString()) / 100;
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                F02 = Convert.ToDouble(dwRule["F02"].ToString());
                DELDATE = dwRule["DELDATE"].ToString();
                STARTDATE = dwRule["STARTDATE"].ToString();
                LDAB50 = Convert.ToDecimal(dwRule["LEADTIME"].ToString());

                POSTYP = Helpers.Helper.Right(dwRule["COLOR"].ToString().Trim().PadRight(3), 3);
                QTYEND = Convert.ToDouble(dwRule["QTYEND"].ToString());
                QTYMAT = Convert.ToDouble(dwRule["QTYMAT"].ToString());
                MU = Helpers.Helper.Left(dwRule["R01DES"].ToString().Trim().PadRight(2), 2);//代表粗纱等定量和内包装
                SMU = Helpers.Helper.Left(dwRule["R02DES"].ToString().Trim().PadRight(2), 2);
                SPACK = Helpers.Helper.Left(dwRule["F01DES"].ToString().Trim().PadRight(4), 4);
            }
            if (QTYEND < 0)
            {
                QTYEND = Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 1000;//无放大系数 
            }
            if (DELDATE.Trim().Length < 8)
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DELDATE;
            }

            if (STARTDATE.Trim().Length < 8)
            {
                STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB50));
            }
            else
            {
                STARTD = STARTDATE;
            }
            if (STARTD.Trim() == "") STARTD = "0";
            double TargetRatio = QTYMAT / QTYEND;//例如1.2，就是100kg成品需要120kg原料
            if (TargetRatio < 0)
                TargetRatio = 1;
            #endregion
            #region=======dwABPN==========
            dwABPN = tmpABPOS.Rows.Add();
            dwABPN["ABPAENDZL"] = 2;
            dwABPN["ABPMANDANT"] = "00";
            dwABPN["ABPAUFART"] = "GP";
            dwABPN["ABPAUFKRS"] = KRS0;
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPZLO"] = PRLO;
            dwABPN["ABPZLB"] = PRLB;
            dwABPN["ABPSME"] = "5";
            dwABPN["ABPPOSTYP"] = POSTYP;
            dwABPN["ABPWAKZ"] = "G";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPVTDATE"] = ENDD;
            dwABPN["ABPENDDAT"] = ENDD;
            dwABPN["ABPENDDATA"] = ENDD;
            dwABPN["ABPLIEFDATN"] = ENDD;
            dwABPN["ABPTERMDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDATA"] = STARTD;
            dwABPN["ABPSTARTDATN"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSOLLMENGE1"] = 0;
            dwABPN["ABPSOLLMENGE2"] = 0;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = Math.Round(QTYEND * 100 * ABRATIO);
            dwABPN["ABPSOLLMENGE6"] = 0;
            dwABPN["ABPSOLLMENGE7"] = Math.Round(VKPQTY * 100 * ABRATIO);
            dwABPN["ABPSOLLMENGE8"] = 0;
            dwABPN["ABPZUGKG"] = 0;
            dwABPN["ABPSTKLG"] = Math.Round(TargetRatio * 10000000);
            //BASEMG2=1.4*0.2
            //GAKG1=ABPQTY*BASEMG2
            #endregion

            #region=======ICC_ABAX ================
            //上层的需求,销售单是上层，细纱单是本层
            var query = from DataRow row in tbABAUFM.AsEnumerable()
                        where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABAQTYRESERVE"] = dwABA["ABARESMG"].ToString();
                row["ABAAUFMSL"] = MU;
            }
            //产生下层需求
            #region============GAAUF732====================
            DataRow dwGASTGA = null;
            double GAKG1 = 0;
            double RT = 0;
            filter = "GAGART='" + ART + "' AND GAGVAR='" + VAR + "' AND GAGFARBE='" + FARBE + "'";
            string sort = " GAGPOS ASC";
            rows = DB.Tables["GASTGA"].Select(filter, sort);
            if (rows.Length == 0)
            {
                MessageBox.Show(filter + "没有BOM");
                throw new Exception(filter + "没有BOM");
            }
            string addedpos = "";
            foreach (DataRow row in rows)
            {
                dwGASTGA = row;

                string YPOS = dwGASTGA["GAGPOS"].ToString();
                if (addedpos.Contains(YPOS + ";"))
                    continue;
                addedpos += YPOS + ";";

                string GART = dwGASTGA["GAGGART"].ToString();
                string GVAR = dwGASTGA["GAGGVAR"].ToString();
                string GFARBE = dwGASTGA["GAGGFARBE"].ToString();
                if (GVAR.Trim() == "")
                {
                    SResult = INTEXDBC.QueryGSCOLOR(dwGASTGA, 7);

                    foreach (DataRow dwColor in SResult.Tables[0].Rows)
                    {
                        GVAR = dwColor["GSCVAR"].ToString();
                    }

                }
                string GAGFILLER = dwGASTGA["GAGFILLER"].ToString();
                if (GAGFILLER.Trim() != "")
                {
                    SMU = GAGFILLER.Substring(1, 2);
                }
                double GAGKGBRUT = Convert.ToDouble(dwGASTGA["GAGANTEIL"].ToString()) * 0.001 * 0.01;
                //BASEMG2=1.4*0.2
                //GAKG1=ABPQTY*BASEMG2

                if (GAGKGBRUT == 0)
                    GAGKGBRUT = 1;
                double GABASMG2 = 0;
                GABASMG2 = TargetRatio * GAGKGBRUT;
                GAKG1 = QTYEND * GABASMG2 * ABRATIO;
                dwMU = tbABAUFM.Rows.Add();
                dwMU["ABAID"] = ABAID; ABAID++;
                #region=====MapRules=======
                dwMU["QTYEND"] = QTYEND;
                dwMU["DELDATE"] = ENDD;
                dwMU["STARTDATE"] = STARTD;
                dwMU["LEADTIME"] = dwRule["LEADTIME"];
                dwMU["QTYMAT"] = GAKG1;
                dwMU["ABACOLORREF"] = dwRule["ORDERNUM"];
                dwMU["UNITNUM"] = dwRule["UNITNUM"];
                dwMU["TTENDS"] = dwRule["TTENDS"];
                dwMU["UENDS"] = dwRule["UENDS"];
                dwMU["UNDES"] = dwRule["UNDES"];
                dwMU["UNITQTY"] = dwRule["UNITQTY"];
                dwMU["R01"] = dwRule["R01"];
                dwMU["R01DES"] = dwRule["R01DES"];
                dwMU["R02"] = dwRule["R02"];
                dwMU["R02DES"] = dwRule["R02DES"];
                dwMU["F01"] = dwRule["F01"];
                dwMU["F01DES"] = dwRule["F01DES"];
                dwMU["F02"] = dwRule["F02"];
                dwMU["F02DES"] = dwRule["F02DES"];
                dwMU["JNUM"] = dwRule["JNUM"];
                dwMU["JWASTGE"] = dwRule["JWASTGE"];
                dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
                dwMU["UMS"] = dwRule["UMS"];
                #endregion
                dwMU["ABAMANDANT"] = "00";
                dwMU["ABAAUFTYP"] = "N";
                dwMU["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFTYP"] = "V";
                dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
                dwMU["ABAVKUPOS"] = dwGASTGA["GAGPOS"];
                dwMU["ABPWAKZ"] = "G";
                dwMU["ABPART"] = ART;
                dwMU["ABPVAR"] = VAR;
                dwMU["ABPFARBE"] = FARBE;
                dwMU["EXART"] = dwABA["EXART"];
                dwMU["ABAWAKZ"] = "G";
                dwMU["ABAART"] = GART;
                dwMU["ABAVAR"] = GVAR;
                dwMU["ABAFARBE"] = GFARBE;
                dwMU["ABASSTKLG"] = 0;
                dwMU["ABARESMG"] = Math.Ceiling(GAKG1 * 1000);
                dwMU["ABASANZSTK"] = Math.Ceiling(GAKG1);
                dwMU["ABASSTKGEW"] = "1000";
                dwMU["ABAAUFMSL"] = SMU; ; //GAAUFMSL
                dwMU["ABAPACKTYP"] = SPACK; ;
                dwMU["ABAHUELSE"] = "      ";
                dwMU["ABAVKFARBE"] = Math.Round(GABASMG2 * 1000 * 100);//100000
                dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
                dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
                dwMU["ABABASMG"] = dwGASTGA["GAGBASIS"];
                dwMU["ABADISPOKZ"] = dwGASTGA["GAGDISPOKZ"];
                dwMU["ABAQTYALLOCATE"] = 0;
                dwMU["ABAQTYRECEIVED"] = 0;
                dwMU["ABALO"] = PRLO;
                dwMU["ABALB"] = PRLB;
                dwMU["ABALP"] = PRLP;
                dwMU["ABAUSER"] = app.User;
                dwMU["ABADATE"] = date; ;
            }
            #endregion
            #endregion
            SReturn.Merge(tmpABPOS);
            return SReturn;
        }

    }
}